using RxjhServer.DbClss;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Timers;

namespace RxjhServer
{
	public class 无限乱斗 : IDisposable
	{
		private System.Timers.Timer 时间1;

		private System.Timers.Timer 时间2;

		private DateTime kssj;

		private DateTime kssjgj;

		private int kssjint;

		private int kssjint1;

		public 无限乱斗()
		{
			try
			{
				if (World.jlMsg == 1)
				{
					Form1.WriteLine(1, "EventClass-");
				}
				kssj = DateTime.Now.AddMinutes(World.乱斗倒计时);
				时间1 = new System.Timers.Timer(10000.0);
				时间1.Elapsed += 时间结束事件1;
				时间1.Enabled = true;
				时间1.AutoReset = true;
				时间结束事件1(null, null);
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "排位赛 EventClass 出错：" + ex?.ToString());
			}
		}

		public void 时间结束事件1(object sender, ElapsedEventArgs e)
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "排位赛系统_时间结束事件1");
			}
			try
			{
				int num = kssjint = (int)kssj.Subtract(DateTime.Now).TotalSeconds;
				World.conn.发送("全服公告|10|排位PK赛|请使用命令  " + World.乱斗命令 + "  参加活动拿丰厚奖品!!!");
				World.conn.发送("全服公告|10|排位PK赛|距离个人排位PK赛开始还剩下[" + num.ToString() + "]秒，亲们准备好了吗？");
				foreach (Players value in World.allConnectedChars.Values)
				{
					value.系统提示("全服公告|10|排位PK赛|请使用命令  " + World.乱斗命令 + "  参加活动拿丰厚奖品!!!", 10, "系统提示");
				}
				if (kssjint <= 0)
				{
					时间1.Enabled = false;
					时间1.Close();
					时间1.Dispose();
					World.乱斗进程 = 1;
					World.发送游戏特殊公告("个人排位PK赛已开始，大侠们可以开始激情的战斗啦！", 10, "系统提示");
					kssjgj = DateTime.Now.AddMinutes(World.乱斗战斗时间);
					时间2 = new System.Timers.Timer(20000.0);
					时间2.Elapsed += 时间结束事件2;
					时间2.Enabled = true;
					时间2.AutoReset = true;
				}
			}
			catch (Exception)
			{
			}
		}

		public void 时间结束事件2(object sender, ElapsedEventArgs e)
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "排位赛系统_时间结束事件2");
			}
			try
			{
				int num = kssjint1 = (int)kssjgj.Subtract(DateTime.Now).TotalSeconds;
				List<LuandouTopClass> list = World.LuandouTop.Values.OrderByDescending((LuandouTopClass a) => a.击杀).Take(3).ToList();
				foreach (Players value in World.allConnectedChars.Values)
				{
					for (int i = 0; i < list.Count; i++)
					{
						int num2 = i + 1;
						if (value.人物坐标_地图 == World.乱斗地图 && World.乱斗进程 == 1)
						{
							value.系统提示("当前PK排位赛第" + num2.ToString() + "名[" + list[i].人物名 + "]", 3, "排位提示");
						}
					}
				}
				if (kssjint1 <= 0)
				{
					时间2.Enabled = false;
					时间2.Close();
					时间2.Dispose();
					World.乱斗进程 = 2;
					DBA.ExeSqlCommand("DELETE FROM luandouTop");
					foreach (LuandouTopClass value2 in World.LuandouTop.Values)
					{
						string sqlCommand = $"INSERT INTO luandouTop (人物名,击杀,死亡)values('{value2.人物名}',{value2.击杀},{value2.死亡})";
						DBA.ExeSqlCommand(sqlCommand);
					}
					World.LuandouTop.Clear();
					乱斗排行奖励();
					乱斗累计奖励();
					World.发送游戏特殊公告("本次排位赛活动结束", 10, "系统提示");
					World.Luandou.Dispose();
				}
			}
			catch (Exception)
			{
			}
		}

		public void 乱斗排行奖励()
		{
			try
			{
				string sqlCommand = "Select TOP 10 * from [luandouTop] Order By 击杀 Desc";
				DataTable dBToDataTable = DBA.GetDBToDataTable(sqlCommand, "GameServer");
				if (dBToDataTable != null)
				{
					if (dBToDataTable.Rows.Count > 0)
					{
						for (int i = 0; i < dBToDataTable.Rows.Count; i++)
						{
							string b = dBToDataTable.Rows[i]["人物名"].ToString();
							foreach (Players value in World.allConnectedChars.Values)
							{
								if (value.UserName == b)
								{
									foreach (乱斗排名类 value2 in World.乱斗排名数据.Values)
									{
										if (i == value2.乱斗排名)
										{
											value.查百宝阁元宝数();
											if (value2.元宝 != 0)
											{
												value.检察元宝数据(value2.元宝, 1);
												value.保存元宝数据();
												value.系统提示("获得乱斗第" + value2.乱斗名次.ToString() + "名，奖励您" + value2.元宝.ToString() + "个元宝！", 10, "乱斗奖励");
											}
											if (value2.武勋 != 0)
											{
												value.Player_WuXun += value2.武勋;
												value.更新武功和状态();
												value.系统提示("获得乱斗第" + value2.乱斗名次.ToString() + "名，奖励您" + value2.武勋.ToString() + "个武勋！", 10, "乱斗奖励");
											}
											World.发送游戏特殊公告("恭喜玩家[" + value.UserName + "]获得本次无限乱斗第" + value2.乱斗名次.ToString() + "名", 10, "系统提示");
										}
									}
								}
							}
						}
					}
					dBToDataTable.Dispose();
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "乱斗系统_奖励事件2|" + ex?.ToString());
			}
		}

		public void 乱斗累计奖励()
		{
			try
			{
				string sqlCommand = "Select * from [luandouTop] where 击杀 > 0  Order By 击杀 Desc";
				DataTable dBToDataTable = DBA.GetDBToDataTable(sqlCommand, "GameServer");
				if (dBToDataTable != null)
				{
					if (dBToDataTable.Rows.Count > 0)
					{
						for (int i = 0; i < dBToDataTable.Rows.Count; i++)
						{
							string b = dBToDataTable.Rows[i]["人物名"].ToString();
							int num = (int)dBToDataTable.Rows[i]["击杀"];
							foreach (Players value in World.allConnectedChars.Values)
							{
								if (value.UserName == b)
								{
									if (num >= 50)
									{
										string[] array = World.累计击杀奖励1.Split(';');
										for (int j = 0; j < int.Parse(array[1]); j++)
										{
											int num2 = value.得到包裹空位位置();
											if (num2 == -1)
											{
												value.系统提示("背包没有空位了， 赶快清理下吧。", 20, "兑换物品");
												return;
											}
											value.增加物品带属性(int.Parse(array[0]), num2, 1, int.Parse(array[2]), int.Parse(array[3]), int.Parse(array[4]), int.Parse(array[5]), int.Parse(array[6]), int.Parse(array[7]));
										}
										value.系统提示("累计击杀奖励" + array[1] + "个宝箱", 10, "累计奖励");
									}
									else if (num < 40 && num >= 20)
									{
										string[] array2 = World.累计击杀奖励2.Split(';');
										for (int k = 0; k < int.Parse(array2[1]); k++)
										{
											int num3 = value.得到包裹空位位置();
											if (num3 == -1)
											{
												value.系统提示("背包没有空位了， 赶快清理下吧。", 20, "兑换物品");
												return;
											}
											value.增加物品带属性(int.Parse(array2[0]), num3, 1, int.Parse(array2[2]), int.Parse(array2[3]), int.Parse(array2[4]), int.Parse(array2[5]), int.Parse(array2[6]), int.Parse(array2[7]));
										}
										value.系统提示("累计击杀奖励" + array2[1] + "个宝箱", 10, "累计奖励");
									}
									else if (num < 20 && num >= 10)
									{
										string[] array3 = World.累计击杀奖励3.Split(';');
										for (int l = 0; l < int.Parse(array3[1]); l++)
										{
											int num4 = value.得到包裹空位位置();
											if (num4 == -1)
											{
												value.系统提示("背包没有空位了， 赶快清理下吧。", 20, "兑换物品");
												return;
											}
											value.增加物品带属性(int.Parse(array3[0]), num4, 1, int.Parse(array3[2]), int.Parse(array3[3]), int.Parse(array3[4]), int.Parse(array3[5]), int.Parse(array3[6]), int.Parse(array3[7]));
										}
										value.系统提示("累计击杀奖励" + array3[1] + "个宝箱", 10, "累计奖励");
									}
									else if (num < 10 && num >= 1)
									{
										string[] array4 = World.累计击杀奖励4.Split(';');
										for (int m = 0; m < int.Parse(array4[1]); m++)
										{
											int num5 = value.得到包裹空位位置();
											if (num5 == -1)
											{
												value.系统提示("背包没有空位了， 赶快清理下吧。", 20, "兑换物品");
												return;
											}
											value.增加物品带属性(int.Parse(array4[0]), num5, 1, int.Parse(array4[2]), int.Parse(array4[3]), int.Parse(array4[4]), int.Parse(array4[5]), int.Parse(array4[6]), int.Parse(array4[7]));
										}
										value.系统提示("累计击杀奖励" + array4[1] + "个宝箱", 10, "累计奖励");
									}
								}
							}
						}
					}
					dBToDataTable.Dispose();
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "排位赛系统_奖励事件3|" + ex?.ToString());
			}
		}

		public void Dispose()
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "EventClass-Dispose");
			}
			World.乱斗进程 = 0;
			if (时间1 != null)
			{
				时间1.Enabled = false;
				时间1.Close();
				时间1.Dispose();
			}
			if (时间2 != null)
			{
				时间2.Enabled = false;
				时间2.Close();
				时间2.Dispose();
			}
			foreach (Players value in World.allConnectedChars.Values)
			{
				if (value.人物坐标_地图 == World.乱斗地图)
				{
					value.移动(1f, 16f, 15f, 1201);
				}
			}
			World.Luandou = null;
		}
	}
}
